package com.ruoyi.zxjd.work.service.impl;

import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.zxjd.work.domain.bo.WorkProgressBO;
import com.ruoyi.zxjd.work.mapper.WorkTemplateProxy;
import com.ruoyi.zxjd.work.service.ICommonWorkTemplateTableService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;

/**
 * @author fjhua
 * @create 2021-04-15 11:20
 */
@Service
public class CommonWorkTemplateTableServiceImpl implements ICommonWorkTemplateTableService {

    private static final Logger log = LoggerFactory.getLogger(CommonWorkTemplateTableServiceImpl.class);

    /**
     * 无id字段添加, 有id字段, 修改
     *
     * @param workProgressBO
     * @param workTemplateTableMethodName 获取  模板上传对象的方法全名
     * @param mapper                      执行sql的mapper对象
     */
    @Override
    public void insertOrUpdate(WorkProgressBO workProgressBO, String workTemplateTableMethodName, Object mapper) {

        BaseEntity workTemplateTable = (BaseEntity) reflectMethod(workProgressBO, workTemplateTableMethodName);

        Long progressId = (Long) reflectMethod(workProgressBO.getWorkProgress(), "getProgressId");
        reflectSetField(workTemplateTable, "setProgressId", progressId, Long.class);
        Long id = null;
        try {
            id = (Long)workTemplateTable.getClass().getMethod("getId").invoke(workTemplateTable);
        } catch (Exception e) {
            log.error(" reflect error", e );
        }
        //获取代理对象
        WorkTemplateProxy workTemplateProxy = WorkTemplateProxy.builder(mapper);
        if (id == null) {
            // id无值, 添加
            workTemplateTable.setCreateBy(ShiroUtils.getLoginName());
            workTemplateTable.setCreateTime(DateUtils.getNowDate());
            workTemplateProxy.insert(workTemplateTable);
        } else {
            //有值,修改
            workTemplateTable.setUpdateBy(ShiroUtils.getLoginName());
            workTemplateTable.setUpdateTime(DateUtils.getNowDate());
            workTemplateProxy.updateById(workTemplateTable);
        }
    }


    private void reflectSetField(Object t, String methodName, Object content, Class contentType) {

        try {
            Method setReadOnly = t.getClass().getMethod(methodName, contentType);
            setReadOnly.invoke(t, content);
        } catch (Exception e) {
            log.error("reflectSetField error", e);
        }
    }

  /*  public static void main(String[] args) {
            WorkTemplateTable8 workTemplateTable81 = new WorkTemplateTable8();
            workTemplateTable81.setId(2L);
            BaseEntity workTemplateTable8 = workTemplateTable81;

            Long id = (Long) reflectMethod(workTemplateTable8, "getId");
            System.out.println(id);


    }*/


    /**
     * 利用反射 获取参数
     *
     * @param object
     * @param methodName
     * @param content
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    private static Object reflectMethod(Object object, String methodName, Object content) {
        try {
            Object result = null;

            Method method = object.getClass().getDeclaredMethod(methodName);
            if (content == null) {

                result = method.invoke(object);
            } else {
                result = method.invoke(object, content);
            }

            return result;
        } catch (Exception e) {
            log.error("reflectMethod error", e);
        }
        return null;
    }

    private static Object reflectMethod(Object object, String methodName) {
        return reflectMethod(object, methodName, null);
    }

}
